package com.itheima.controller;

import com.itheima.entity.Result;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

@RestController
@RequestMapping("/validateCode")
public class validateCodeController {
    @Autowired
    private JedisPool jedisPool;


  @PostMapping("/send4Login")
    public Result send4Login(String telephone){
      int code = RandomUtils.nextInt(100000, 999999);
      System.out.println(code);
      /*   boolean flag = yunSmsUtils.sendYunzhixunMsg(telephone, code, 5 * 60);*/
      /*  if (flag) {*/
      Jedis jedis = null;
      try {
          jedis = jedisPool.getResource();
          jedis.setex("LOGIN" + telephone, 5 * 60, String.valueOf(code));
      } catch (Exception e) {
          e.printStackTrace();
      } finally {
          if (jedis != null) {
              jedis.close();
          }
      }
      /*  }*/

      return Result.success(true);
    }

}
